<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
        }

        /* 导航栏样式 */
        .nav {
            background-color: #2c3e50;
            padding: 15px 0;
            margin-bottom: 30px;
            text-align: center;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            position: relative;
        }

        .nav a {
            color: white;
            text-decoration: none;
            margin: 0 15px;
            padding: 8px 15px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .nav a:hover {
            background-color: #34495e;
            border-radius: 4px;
        }

        .nav a.active {
            background-color: #3498db;
            border-radius: 4px;
        }

        /* 退出按钮样式 */
        .logout-btn {
            position: absolute;
            right: 20px;
            top: 50%;
            transform: translateY(-50%);
            background-color: #e74c3c;
            color: white;
            padding: 8px 15px;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .logout-btn:hover {
            background-color: #c0392b;
        }

        /* 内容区域样式 */
        .container {
            width: 80%;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        /* 表单样式 */
        .search-form {
            margin-bottom: 20px;
            text-align: center;
        }

        .search-form input[type="text"] {
            padding: 8px 15px;
            width: 200px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-right: 10px;
            font-size: 14px;
        }

        .search-form input[type="submit"],
        button {
            padding: 8px 20px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.3s ease;
        }

        .search-form input[type="submit"]:hover,
        button:hover {
            background-color: #2980b9;
        }

        /* 表格样式 */
        table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
            margin-top: 20px;
        }

        th,
        td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #2c3e50;
            color: white;
            font-weight: normal;
        }

        tr:hover {
            background-color: #f9f9f9;
        }

        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
        }

        .modal-content {
            background-color: white;
            margin: 10% auto;
            padding: 25px;
            border-radius: 5px;
            width: 50%;
            max-width: 500px;
            box-shadow: 0 3px 6px rgba(0, 0, 0, 0.2);
        }

        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
            transition: color 0.3s ease;
        }

        .close:hover {
            color: #2c3e50;
        }

        /* 表单元素样式 */
        .modal-content form {
            margin-top: 20px;
        }

        .modal-content label {
            display: block;
            margin-bottom: 8px;
            color: #2c3e50;
        }

        .modal-content input[type="text"],
        .modal-content input[type="password"],
        .modal-content select {
            width: 100%;
            padding: 8px;
            margin-bottom: 20px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .modal-content input[type="submit"] {
            background-color: #3498db;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
            font-size: 14px;
        }

        .modal-content input[type="submit"]:hover {
            background-color: #2980b9;
        }

        /* 操作链接样式 */
        .action-link {
            color: #3498db;
            text-decoration: none;
            margin: 0 5px;
            transition: color 0.3s ease;
        }

        .action-link:hover {
            color: #2980b9;
        }

        .delete-link {
            color: #e74c3c;
        }

        .delete-link:hover {
            color: #c0392b;
        }
    </style>
</head>

<body>
<!-- 导航栏 -->
<div class="nav">
    <a href="index.html">学生管理</a>
    <a href="user.html" class="active">用户管理</a>
    <a href="major.html">专业管理</a>
    <a href="javascript:void(0)" class="logout-btn" onclick="logout()">退出登录</a>
</div>

<div class="container">
    <div class="search-form">
        <form id="searchForm" action="user/query" method="POST">
            <input type="text" name="username" placeholder="请输入用户名" value="">
            <input type="submit" value="查询">
        </form>
        <p>
            <button id="addButton">添加用户</button>
        </p>
    </div>

    <table id="userTable">
        <thead>
        <tr>
            <th>用户ID</th>
            <th>用户名</th>
            <th>账号</th>
            <th>权限</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <!-- 这里会动态填充用户信息 -->
        </tbody>
    </table>
</div>

<!-- 编辑模态框 -->
<div id="editModal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <h2>编辑用户信息</h2>
        <form id="editForm">
            <input type="hidden" id="editId">

            <label for="editUsername">用户名:</label>
            <input type="text" id="editUsername" required>

            <label for="editAccount">账号:</label>
            <input type="text" id="editAccount" required>

            <label for="editPassword">密码:</label>
            <input type="password" id="editPassword" placeholder="不修改请留空">

            <label for="editPermission">权限:</label>
            <select id="editPermission" required>
                <option value="1">管理员</option>
                <option value="0">普通用户</option>
            </select>

            <input type="submit" value="保存">
        </form>
    </div>
</div>

<!-- 添加用户模态框 -->
<div id="addModal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <h2>添加用户信息</h2>
        <form id="addForm">
            <label for="addUsername">用户名:</label>
            <input type="text" id="addUsername" required>

            <label for="addAccount">账号:</label>
            <input type="text" id="addAccount" required>

            <label for="addPassword">密码:</label>
            <input type="password" id="addPassword" required>

            <label for="addPermission">权限:</label>
            <select id="addPermission" required>
                <option value="1">管理员</option>
                <option value="0">普通用户</option>
            </select>

            <input type="submit" value="保存">
        </form>
    </div>
</div>

<script>
    window.onload = function () {
        // 添加退出登录函数
        window.logout = function () {
            if (confirm('确定要退出登录吗？')) {
                window.location.href = 'login.html';
            }
        };

        // 获取用户权限
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'getPermission', true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                var permission = parseInt(xhr.responseText);
                if (permission === 0) {
                    // 普通用户隐藏添加、编辑和删除按钮
                    document.getElementById('addButton').style.display = 'none';
                }
            }
        };
        xhr.send();

        // 加载用户列表
        function loadUsers() {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', 'user/query', true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var table = document.getElementById('userTable');
                    // 保留表头
                    while (table.rows.length > 1) {
                        table.deleteRow(1);
                    }
                    table.innerHTML += xhr.responseText;
                    addEditDeleteListeners();
                }
            };
            xhr.send();
        }

        // 页面加载时获取用户列表
        loadUsers();

        // 搜索表单提交
        document.getElementById('searchForm').addEventListener('submit', function (e) {
            e.preventDefault();
            var username = document.querySelector('#searchForm input[name="username"]').value;

            var xhrPost = new XMLHttpRequest();
            xhrPost.open('POST', 'user/query', true);
            xhrPost.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
            xhrPost.onreadystatechange = function () {
                if (xhrPost.readyState === 4) {
                    if (xhrPost.status === 200) {
                        var table = document.getElementById('userTable');
                        // 保留表头
                        while (table.rows.length > 1) {
                            table.deleteRow(1);
                        }
                        table.innerHTML += xhrPost.responseText;
                        addEditDeleteListeners();
                    } else {
                        alert("查询失败：" + xhrPost.responseText);
                    }
                }
            };
            var params = 'username=' + encodeURIComponent(username);
            xhrPost.send(params);
        });

        function addEditDeleteListeners() {
            var editLinks = document.querySelectorAll('.edit-link');
            var deleteLinks = document.querySelectorAll('.delete-link');

            // 获取用户权限
            var xhr = new XMLHttpRequest();
            xhr.open('GET', 'getPermission', true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var permission = parseInt(xhr.responseText);
                    if (permission === 0) {
                        // 普通用户隐藏编辑和删除链接
                        editLinks.forEach(function (link) {
                            link.style.display = 'none';
                        });
                        deleteLinks.forEach(function (link) {
                            link.style.display = 'none';
                        });
                    } else {
                        editLinks.forEach(function (link) {
                            link.addEventListener('click', function (e) {
                                e.preventDefault();
                                var id = this.getAttribute('data-id');
                                var row = this.parentNode.parentNode;
                                var username = row.cells[1].textContent;
                                var account = row.cells[2].textContent;
                                var permission = row.cells[3].textContent === '管理员' ? '1' : '0';

                                document.getElementById('editId').value = id;
                                document.getElementById('editUsername').value = username;
                                document.getElementById('editAccount').value = account;
                                document.getElementById('editPermission').value = permission;
                                document.getElementById('editPassword').value = '';

                                var modal = document.getElementById('editModal');
                                modal.style.display = "block";
                            });
                        });

                        deleteLinks.forEach(function (link) {
                            link.addEventListener('click', function (e) {
                                e.preventDefault();
                                var id = this.getAttribute('data-id');
                                if (confirm('确定要删除此用户吗？')) {
                                    var xhrDelete = new XMLHttpRequest();
                                    xhrDelete.open('GET', 'user/delete?id=' + id, true);
                                    xhrDelete.onreadystatechange = function () {
                                        if (xhrDelete.readyState === 4 && xhrDelete.status === 200) {
                                            loadUsers();
                                        }
                                    };
                                    xhrDelete.send();
                                }
                            });
                        });
                    }
                }
            };
            xhr.send();
        }

        // 添加用户按钮点击事件
        document.getElementById('addButton').addEventListener('click', function () {
            var modal = document.getElementById('addModal');
            modal.style.display = "block";
        });

        // 关闭模态框
        var spans = document.getElementsByClassName("close");
        Array.from(spans).forEach(function (span) {
            span.onclick = function () {
                this.closest('.modal').style.display = "none";
            }
        });

        // 提交编辑表单
        document.getElementById('editForm').addEventListener('submit', function (e) {
            e.preventDefault();
            var id = document.getElementById('editId').value;
            var username = document.getElementById('editUsername').value;
            var account = document.getElementById('editAccount').value;
            var password = document.getElementById('editPassword').value;
            var permission = document.getElementById('editPermission').value;

            var xhrEdit = new XMLHttpRequest();
            xhrEdit.open('POST', 'user/edit', true);
            xhrEdit.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhrEdit.onreadystatechange = function () {
                if (xhrEdit.readyState === 4) {
                    if (xhrEdit.status === 200) {
                        document.getElementById('editModal').style.display = "none";
                        loadUsers();
                    } else {
                        alert("修改失败：" + xhrEdit.responseText);
                    }
                }
            };
            var params = 'id=' + encodeURIComponent(id) +
                '&username=' + encodeURIComponent(username) +
                '&account=' + encodeURIComponent(account) +
                (password ? '&password=' + encodeURIComponent(password) : '') +
                '&permission=' + encodeURIComponent(permission);
            xhrEdit.send(params);
        });

        // 提交添加表单
        document.getElementById('addForm').addEventListener('submit', function (e) {
            e.preventDefault();
            var username = document.getElementById('addUsername').value;
            var account = document.getElementById('addAccount').value;
            var password = document.getElementById('addPassword').value;
            var permission = document.getElementById('addPermission').value;

            var xhrAdd = new XMLHttpRequest();
            xhrAdd.open('POST', 'user/add', true);
            xhrAdd.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhrAdd.onreadystatechange = function () {
                if (xhrAdd.readyState === 4) {
                    if (xhrAdd.status === 200) {
                        document.getElementById('addModal').style.display = "none";
                        document.getElementById('addForm').reset();
                        loadUsers();
                    } else {
                        alert("添加失败：" + xhrAdd.responseText);
                    }
                }
            };
            var params = 'username=' + encodeURIComponent(username) +
                '&account=' + encodeURIComponent(account) +
                '&password=' + encodeURIComponent(password) +
                '&permission=' + encodeURIComponent(permission);
            xhrAdd.send(params);
        });
    };
</script>
</body>

</html>